package com.kx.project.exam.mapper;

import java.util.List;

import com.kx.project.exam.domain.Question;
import org.apache.ibatis.annotations.*;

/**
 * 题目 mapper组件
 * 
 * @author kx  
 */
@Mapper
public interface QuestionMapper {

    /**
     * 分页查询
     *
     * @param question 题目信息
     * @return 题目集合
     */
    @Select("<script>"
            + "SELECT "
                + "id,"
                + "question_name,"
                + "question_type,"
                + "is_keypoint,"
                + "create_by,"
                + "create_time, "
                + "update_by,"
                + "update_time, "
                + "remark "
            + "FROM exam_question "
            + "WHERE 1=1 "
                + "<if test=\"questionName != null and questionName != ''\">"
                + "AND question_name like '%${questionName}%' "
                + "</if>"

                + "<if test=\"questionType != null and questionType != ''\">"
                + "AND question_type=#{questionType} "
                + "</if>"

                + "<if test=\"isKeypoint != null and isKeypoint != ''\">"
                + "AND is_keypoint=#{isKeypoint} "
                + "</if>"

                + "<if test=\"beginTime != null and beginTime != ''\">"
                + "AND create_time &gt;= #{beginTime,jdbcType=TIMESTAMP} "
                + "</if>"

                + "<if test=\"endTime != null and endTime != ''\">"
                + "AND create_time &lt;= #{endTime,jdbcType=TIMESTAMP} "
                + "</if>"
            + "</script>")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "question_name", property = "questionName"),
            @Result(column = "question_type", property = "questionType"),
            @Result(column = "is_keypoint", property = "isKeypoint"),
            @Result(column = "create_by", property = "createBy"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_by", property = "updateBy"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "remark", property = "remark")
    })
    List<Question> listByPage(Question question);

    /**
     * 查询题目
     * 
     * @param question 题目信息
     * @return 题目信息
     */
    @Select("SELECT "
            + "id,"
            + "question_name,"
            + "question_type,"
            + "is_keypoint,"
            + "create_by,"
            + "create_time, "
            + "update_by,"
            + "update_time, "
            + "remark "
                + "FROM exam_question "
            + "WHERE id=#{id}")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "question_name", property = "questionName"),
            @Result(column = "question_type", property = "questionType"),
            @Result(column = "is_keypoint", property = "isKeypoint"),
            @Result(column = "create_by", property = "createBy"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_by", property = "updateBy"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "remark", property = "remark")
    })
    Question getById(Question question);

    /**
     * 新增题目
     * 
     * @param question 题目信息
     * @return 结果
     */
    @Insert("INSERT INTO exam_question("
                + "question_name,"
                + "is_keypoint,"
                + "question_type,"
                + "create_time,"
                + "create_by,"
                + "update_time,"
                + "update_by,"
                + "remark"
            + ") VALUES ("
                + "#{questionName},"
                + "#{isKeypoint},"
                + "#{questionType},"
                + "#{updateTime},"
                + "#{updateBy},"
                + "#{createTime},"
                + "#{createBy},"
                + "#{remark}"
            + ")")
    @Options(keyColumn = "id", keyProperty = "id", useGeneratedKeys = true)
    int insert(Question question);

    /**
     * 修改题目
     * 
     * @param question 题目信息
     * @return 结果
     */
    @Update("UPDATE exam_question SET "
                + "question_name=#{questionName},"
                + "is_keypoint=#{isKeypoint},"
                + "question_type=#{questionType},"
                + "update_time=#{updateTime},"
                + "update_by=#{updateBy},"
                + "remark=#{remark} "
            + "WHERE id=#{id}")
    int update(Question question);

    /**
     * 删除题目
     * 
     * @param id 参数ID
     * @return 结果
     */
    @Delete("DELETE FROM exam_question WHERE id=#{id}")
    void removeById(@Param("id") Long id);

    /**
     * 检查唯一
     *
     * @param questionName 检查关键字
     * @return 题目信息
     */
    @Select("SELECT "
                + "id,"
                + "question_name, "
                + "question_type, "
                + "is_keypoint, "
                + "create_by, "
                + "create_time, "
                + "update_by, "
                + "update_time, "
                + "remark "
            + "FROM exam_question "
            + "WHERE question_name=#{questionName} "
            + "Limit 1")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "question_name", property = "questionName"),
            @Result(column = "question_type", property = "questionType"),
            @Result(column = "is_keypoint", property = "isKeypoint"),
            @Result(column = "create_by", property = "createBy"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_by", property = "updateBy"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "remark", property = "remark")
    })
    Question checkUnique(@Param("questionName") String questionName);
}